package com.example.demo.demos.web;

import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class LoginController {

    // 模拟数据库存储用户信息
    private static final Map<String, User> userDatabase = new HashMap<>();

    // 初始化一些测试用户
    static {
        User admin = new User();
        admin.setUserId("1");
        admin.setUsername("admin");
        admin.setPassword("123456");
        userDatabase.put("admin", admin);

        User testUser = new User();
        testUser.setUserId("2");
        testUser.setUsername("test");
        testUser.setPassword("test123");
        userDatabase.put("test", testUser);
    }

    /**
     * 用户登录接口 (GET请求版本)
     */
    @GetMapping("/login")
    public ResponseData login(@RequestParam String username, @RequestParam String password) {
        System.out.println("username: " + username + " password: " + password);
        // 参数校验
        if (username == null || username.trim().isEmpty()) {
            return ResponseData.error(400, "用户名不能为空");
        }
        if (password == null || password.trim().isEmpty()) {
            return ResponseData.error(400, "密码不能为空");
        }

        // 模拟数据库查询用户
        User user = userDatabase.get(username);

        // 验证用户是否存在且密码正确
        if (user != null && password.equals(user.getPassword())) {
            // 登录成功，返回用户基本信息（不返回密码）
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(user.getUserId());
            userInfo.setUsername(user.getUsername());
            return ResponseData.success(userInfo);
        } else {
            return ResponseData.error(401, "用户名或密码错误");
        }
    }

    /**
     * 用户注册接口 (GET请求版本)
     */
    @GetMapping("/register")
    public ResponseData register(@RequestParam String username, @RequestParam String password) {
        // 参数校验
        if (username == null || username.trim().isEmpty()) {
            return ResponseData.error(400, "用户名不能为空");
        }
        if (password == null || password.trim().isEmpty()) {
            return ResponseData.error(400, "密码不能为空");
        }
        if (password.length() < 6) {
            return ResponseData.error(400, "密码长度不能少于6位");
        }

        // 检查用户名是否已存在
        if (userDatabase.containsKey(username)) {
            return ResponseData.error(400, "用户名已存在");
        }

        // 创建新用户
        User newUser = new User();
        newUser.setUserId(String.valueOf(userDatabase.size() + 1));
        newUser.setUsername(username);
        newUser.setPassword(password);

        // 模拟保存到数据库
        userDatabase.put(newUser.getUsername(), newUser);

        // 返回注册成功的用户信息
        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(newUser.getUserId());
        userInfo.setUsername(newUser.getUsername());

        return ResponseData.success(userInfo);
    }

    /**
     * 获取用户信息接口
     */
    @GetMapping("/info")
    public ResponseData getUserInfo(@RequestParam String userId) {
        // 参数校验
        if (userId == null || userId.trim().isEmpty()) {
            return ResponseData.error(400, "用户ID不能为空");
        }

        // 模拟根据用户ID查询用户信息
        User foundUser = null;
        for (User user : userDatabase.values()) {
            if (userId.equals(user.getUserId())) {
                foundUser = user;
                break;
            }
        }

        if (foundUser != null) {
            UserInfo userInfo = new UserInfo();
            userInfo.setUserId(foundUser.getUserId());
            userInfo.setUsername(foundUser.getUsername());
            return ResponseData.success(userInfo);
        } else {
            return ResponseData.error(404, "用户不存在");
        }
    }

    /**
     * 获取所有用户列表
     */
    @GetMapping("/list")
    public ResponseData getAllUsers() {
        return ResponseData.success(userDatabase.values());
    }

    // 内部类定义保持不变
    static class LoginRequest {
        private String username;
        private String password;

        public LoginRequest() {}

        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

    static class RegisterRequest {
        private String username;
        private String password;

        public RegisterRequest() {}

        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

    static class User {
        private String userId;
        private String username;
        private String password;

        public User() {}

        public String getUserId() { return userId; }
        public void setUserId(String userId) { this.userId = userId; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
        public String getPassword() { return password; }
        public void setPassword(String password) { this.password = password; }
    }

    static class UserInfo {
        private String userId;
        private String username;

        public UserInfo() {}

        public String getUserId() { return userId; }
        public void setUserId(String userId) { this.userId = userId; }
        public String getUsername() { return username; }
        public void setUsername(String username) { this.username = username; }
    }

    static class ResponseData {
        private int code;
        private String message;
        private Object data;

        public ResponseData() {}

        public int getCode() { return code; }
        public void setCode(int code) { this.code = code; }
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
        public Object getData() { return data; }
        public void setData(Object data) { this.data = data; }

        public static ResponseData success(Object data) {
            ResponseData response = new ResponseData();
            response.code = 200;
            response.message = "操作成功";
            response.data = data;
            return response;
        }

        public static ResponseData error(int code, String message) {
            ResponseData response = new ResponseData();
            response.code = code;
            response.message = message;
            response.data = null;
            return response;
        }
    }
}
